//保存所有的登录状态
// 引入路由
import router from '@/router';
//引入 登录api
import {$accountLogin} from '@/api/index.js';

const user = {
    state: {
        account: "",
        username: "",
        type: "",
        token: "",
    },
    mutations: {
        // 用户名
        SET_ACCOUNT(state, account) {
            state.account = account;
        },
        SET_USERNAME(state, username) {
            state.username = username;
        },
        SET_TYPE(state, type) {
            state.type = type;
        },
        SET_TOKEN(state, token) {
            state.token = token;
        },
    },
    actions: {
        LoginAction({commit}, userInfo) {
            $accountLogin(userInfo).then(res => {
                console.log(res);
                if (res.data.code === 666) {
                    //设置 mutations
                    console.log("登录成功");
                    console.log(commit);
                    commit('SET_ACCOUNT', res.data.data.account);
                    commit('SET_USERNAME', res.data.data.username);
                    commit('SET_TYPE', res.data.data.type);
                    commit('SET_TOKEN', res.data.token);
                    //路由跳转到首页
                    router.push('/home/dashboard');
                }
            }).catch(error => {
                    console.log(error);
                })
        },
        LogoutAction({ commit }) {
            console.log("退出", commit);
            commit('SET_ACCOUNT',"");
            commit('SET_USERNAME',"");
            commit('SET_TYPE',"");
            commit('SET_TOKEN',"");
            router.push('/login');
        }
    }
}
export default user;